<?php

namespace App\Console\Commands;

use CustomerForm\CustomerFormEngine;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
use PhpOffice\PhpSpreadsheet\IOFactory;
use ScarecrowExcel\ExcelHandle;

class ExampleCommand extends Command
{
    /**
     * command name
     */
    protected $signature = 'ExampleCommand';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'This ExampleCommand!';

    /**
     * Handle Function!
     */
    public function handle()
    {
        echo "This lumen base frame version is: " . GetVersion();
    }

    public function test($inputFileName){
        $imageFilePath = storage_path('app/uploads/'); //图片本地存储的路径
        if (!file_exists($imageFilePath)) { //如果目录不存在则递归创建
            mkdir($imageFilePath, 0777, true);
        }
        try {
            $objRead = IOFactory::createReader('Xlsx');
            $objSpreadsheet = $objRead->load($inputFileName);
            $objWorksheet = $objSpreadsheet->getSheet(0);
            $data = $objWorksheet->toArray();
            foreach ($objWorksheet->getDrawingCollection() as $drawing) {
                list($startColumn, $startRow) = Coordinate::coordinateFromString($drawing->getCoordinates());
                $imageFileName = $drawing->getCoordinates() . mt_rand(1000, 9999);
//                switch ($drawing->getExtension()) {
//                    case 'jpg':
//                    case 'JPG':
//                    case 'jpeg':
//                    case 'JPEG':
//                        $imageFileName .= '.jpg';
//                        $source = imagecreatefromjpeg($drawing->getPath());
//                        imagejpeg($source, $imageFilePath . $imageFileName);
//                        break;
//                    case 'gif':
//                    case 'GIF':
//                        $imageFileName .= '.gif';
//                        $source = imagecreatefromgif($drawing->getPath());
//                        imagegif($source, $imageFilePath . $imageFileName);
//                        break;
//                    case 'png':
//                    case 'PNG':
//                        $imageFileName .= '.png';
//                        $source = imagecreatefrompng($drawing->getPath());
//                        imagepng($source, $imageFilePath . $imageFileName);
//                        break;
//                }
                var_dump($startColumn);
                $startColumn = $this->ABC2decimal($startColumn);
                DieDump($startColumn);
                $data[$startRow-1][$startColumn] = $imageFilePath . $imageFileName;
            }
        } catch (\Exception $e) {
            throw $e;
        }
    }

    public function ABC2decimal($abc)
    {
        $ten = 0;
        $len = strlen($abc);
        for($i=1;$i<=$len;$i++){
            $char = substr($abc,0-$i,1);//反向获取单个字符
            $int = ord($char);
            $ten += ($int-65)*pow(26,$i-1);
        }
        return $ten;
    }
}
